# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://docs.scrapy.org/en/latest/topics/item-pipeline.html


# useful for handling different item types with a single interface


import csv
from spider.items import StockClassItem, StockItem


class SpiderPipeline:
    def __init__(self):
        self.file = None
        self.writer = None

    def open_spider(self, spider):
        self.file = open('Stock_Data.csv', 'w', newline='', encoding='utf-8')
        self.writer = csv.writer(self.file)
        self.writer.writerow(
            ('行业代码', '行业名称', '股票代码', '股票名称', '当前价', '涨跌额', '涨跌幅', '年初至今', '成交量', '成交额', '换手率',
            '股息率', '市值'))

    def close_spider(self, spider):
        if self.file:
            self.file.close()

    def process_item(self, item, spider):
        if isinstance(item, StockItem):
            self.writer.writerow((item['Ccode'], item['Cname'], item['symbol'], item['name'], item['current'], item['chg'], item['percent'],
                                   item['current_year_percent'], item['volume'], item['amount'], item['turnover_rate'],
                                   item['dividend_yield'], item['market_capital']))
        return item

class CsvPipeline:
    def __init__(self):
        self.file = None
        self.writer = None

    def open_spider(self, spider):
        self.file = open('stocks.csv', mode='a', newline='', encoding='utf-8')
        self.writer = csv.writer(self.file)
        self.writer.writerow(['Ccode', 'Cname'])

    def close_spider(self, spider):
        if self.file:
            self.file.close()

    def process_item(self, item, spider):
        if isinstance(item, StockClassItem):
            self.writer.writerow([item['level2code'], item['title']])
        return item